package com.onlyyou.custom.controller;

import com.onlyyou.custom.VO.ResultVO;
import com.onlyyou.custom.entity.Custom;
import com.onlyyou.custom.info.CustomizeInfo;
import com.onlyyou.custom.service.CustomService;
import com.onlyyou.custom.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author: 小野
 * @Date: 2020/1/15 10:44
 */
@RestController
@RequestMapping("/")
@Api(value = "定制服务接口")
public class CustomController {
    @Autowired
    private CustomService customService;
    @ApiOperation(value="查询所有定制订单")
    @GetMapping("/queryAll")
    public ResultVO queryAll(@PathVariable Integer pages,@PathVariable Integer rows){
        return ResultUtil.success(customService.findAll(pages, rows));
    }
    @ApiOperation(value="根据ID查询定制订单")
    @GetMapping("/queryById/{Id}")
    public ResultVO queryById(@PathVariable("Id") Long Id){
        return ResultUtil.success(customService.findById(Id));
    }
    @ApiOperation(value = "根据用户ID查询定制订单")
    @GetMapping("/queryByUserId/{userId}")
    public ResultVO queryByUserId(@PathVariable("userId") Long uerId) {
        return ResultUtil.success(customService.findByUserId(uerId));
    }

    @ApiOperation(value = "根据商家用户ID查询定制订单")
    @GetMapping("/queryByOrderUserId/{orderUserId}")
    public ResultVO queryByOrderUserId(@PathVariable("orderUserId") Long orderUserId) {
        return ResultUtil.success(customService.findByOrderUserId(orderUserId));
    }

    @ApiOperation(value = "根据用户提交定制订单")
    @PostMapping("/save")
    public ResultVO save(String cpu,String mother,String grphics,String RAM,String hard,String heat,String monitor,String keyboard,
                         String mouse,String service,Long userId) {
        CustomizeInfo customize=new CustomizeInfo();
        customize.setCpu(cpu);
        customize.setHard(hard);
        customize.setGrphics(grphics);
        customize.setHeat(heat);
        customize.setKeyboard(keyboard);
        customize.setMother(mother);
        customize.setMouse(mouse);
        customize.setService(service);
        customize.setMonitor(monitor);
        customize.setRAM(RAM);
        Custom custom=new Custom();
        custom.setUserId(userId);
        custom.setCustomDescribe(customize.toString());
        custom.setId(System.currentTimeMillis());
        custom.setCreateTime(new Date());
        return ResultUtil.success(customService.save(custom));
    }

    @ApiOperation(value = "根据用户删除定制订单")
    @GetMapping("/delete")
    public ResultVO delete(@RequestParam Long id,@RequestParam Long userId) {
        Custom custom=new Custom();
        custom.setId(id);
        custom.setUserId(userId);
        customService.delete(custom);
        return ResultUtil.success();
    }


}
